
#import "HZSuggestHeaderView.h"



#define TextViewMaxLength 200

@interface HZSuggestHeaderView()<UITextViewDelegate>
@property(nonatomic,strong)UITextView * textfield;
@property(nonatomic,strong)UILabel * Label;
@property(nonatomic,strong)UIButton * button;
//用于记录label可以输入的字个数
@property(nonatomic,assign)int numbel;
@end

@implementation HZSuggestHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return self;
}

#pragma mark -
#pragma mark 设置界面
-(void)setupUI{
    //默认为0;
    self.numbel = 0;
    
    /**
     *  添加子控件
     */
    [self addSubview:self.textfield];
    [self addSubview:self.Label];
    [self addSubview:self.button];
    
   
    
    //约束
    [self.textfield makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self).offset(-10);
        make.leading.equalTo(self).offset(10);
        make.top.equalTo(self).offset(20);
        make.height.equalTo(200);
    }];
    
    [self.button makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.textfield);
        make.top.equalTo(self.textfield.bottom);
        make.height.equalTo(30);
        make.width.equalTo(80);
        
        
    }];
    
    [self.Label makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.equalTo(self.button.leading).offset(-30);
        make.centerY.equalTo(self.button);
        make.height.equalTo(self.button);
        make.width.equalTo(150);
        
        
    }];
    
    //textfield改变的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    
}

#pragma mark -
#pragma mark button的点击事件
-(void)didClickBtn{
    
    if (self.textfield.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"反馈不能为空,请重新输入"];
        
    }else{
        [SVProgressHUD showSuccessWithStatus:@"提交成功"];
    }
    
   
    
}

#pragma mark -
#pragma mark textfield的通知方法
-(void)textDidChange{
    
    NSString * text = self.textfield.text;
    self.numbel += (int)text.length;
    
    self.Label.text = [NSString stringWithFormat:@"你还可以输入%d个字",TextViewMaxLength-self.numbel];

    self.numbel = 0;
    
    
    
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if ([text isEqualToString:@""]) {
        return YES;
    }
    if (self.textfield.text.length >= TextViewMaxLength) {
        return NO;
    }else{
        return YES;
    }
    return YES;
}



#pragma mark -
#pragma mark 懒加载控件
-(UITextView *)textfield{
    if (!_textfield) {
        _textfield = [[UITextView alloc]init];
        _textfield.delegate = self;
        
//        _textfield.backgroundColor = [UIColor orangeColor];
        
    }
    return _textfield;
}

-(UILabel *)Label{
    if (!_Label) {
        _Label = [[UILabel alloc]init];
        _Label.font = [UIFont systemFontOfSize:14];
        _Label.text = [NSString stringWithFormat:@"您还可以输入%d个字",TextViewMaxLength-self.numbel];
//        _Label.backgroundColor = [UIColor purpleColor];
    }
    return _Label;
}

-(UIButton *)button{
    if (!_button) {
        _button = [[UIButton alloc]init];
        
        [_button addTarget:self action:@selector(didClickBtn) forControlEvents:UIControlEventTouchUpInside];
        [_button setTitle:@"递交反馈" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _button.titleLabel.font = [UIFont systemFontOfSize:15];
        [_button setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
        
    }
    return _button;
}

@end
